<?php

namespace app\api\controller;

use app\admin\model\ConfigValueModel;
use app\common\controller\Api;
use app\common\exception\UploadException;
use app\admin\model\Dengji;
use app\admin\model\Suborder;
use think\Config;
use app\common\library\Upload;
use think\Db;

/**
 * 首页接口
 */
class Index extends Api
{
    //protected $noNeedLogin = ['*'];
    protected $noNeedLogin = ['index','yunyingshang','arealist','taocan','detail','content','notice','wenti','upload','check','cun','jiedao','qu','zu_qrcode','contract','wenti'];
    protected $noNeedRight = ['*'];


    /**
     * 首页
     *
     */
    public function index()
    {
        $list = Db::table('fa_link')->where("state","1")->select();
        $list = collection($list)->toArray();
        foreach($list as $k=>&$v){
            $v['image'] = request()->domain().$v['image'];
        }

        //($list);die();
        $msg = Db::table('fa_msg')
            ->where("state","1")
            ->order('id desc')
            ->find();
        $msg = $msg?$msg['title']:'暂无公告内容，可稍后再关注';
        $data = [
            'notice' => [
                $msg,
            ],
            'list' => $list
        ];
        $this->success('请求成功',$data);
    }

    public function yunyingshang()
    {
        $data = [
            'yidong'=>'中国移动','liantong'=>'中国联通','dianxin'=>'中国电信','guangdian'=>'中国广电',
        ];
        $this->success('请求成功',$data);
    }
    public function arealist()
    {
        $list = Db::table('fa_village')->where('state','1')->select();
        foreach($list as $k=>&$v ){
            $v['arearname'] = $v['name'];
            $v['areaid'] = $v['id'];
        }
        $this->success('请求成功',$list);
    }
    public function taocan()
    {
        $where = [];
        $where['state'] = ['=','1'];
        if(!empty(input('keywords'))){
            $where['taocan'] = ['like','%'.input('keywords').'%'];
        }
        if(!empty(input('yunying_type'))){
            $where['yunying'] = ['=',input('yunying_type')] ;
        }
        if(!empty(input('arear_id'))){
            $where['village_id'] = ['=',input('arear_id')] ;
        }
        $data = Db::table('fa_taocan')->where($where)->paginate()->toArray();
        //print_r($data);die();
        foreach ($data['data'] as $k=>&$item){
            $item['yunying_text'] = '';
            if($item['yunying'] == 'yidong'){
                $item['yunying_text'] = '中国移动';
            }
            if($item['yunying'] == 'liantong'){
                $item['yunying_text'] = '中国联通';
            }
            if($item['yunying'] == 'dianxin'){
                $item['yunying_text'] = '中国电信';
            }
            if($item['yunying'] == 'guangdian'){
                $item['yunying_text'] = '中国广电';
            }
            $item['image'] = request()->domain().'/img/2.png';
        }
//        ->each(function($item){
//            $item['yunying_text'] = '';
//            if($item['yunying'] == 'yidong'){
//                $item['yunying_text'] = '中国移动';
//            }
//            if($item['yunying'] == 'liantong'){
//                $item['yunying_text'] = '中国联通';
//            }
//            if($item['yunying'] == 'dianxin'){
//                $item['yunying_text'] = '中国电信';
//            }
//            if($item['yunying'] == 'guangdian'){
//                $item['yunying_text'] = '中国广电';
//            }
//        });

        $this->success('请求成功',$data);
    }

    public function detail()
    {
        $item = Db::table('fa_taocan')->find(input('id'));
        if(empty($item)){
            $this->error('参数错误');
        }
        $item['yunying_text'] = '';
        if($item['yunying'] == 'yidong'){
            $item['yunying_text'] = '中国移动';
        }
        if($item['yunying'] == 'liantong'){
            $item['yunying_text'] = '中国联通';
        }
        if($item['yunying'] == 'dianxin'){
            $item['yunying_text'] = '中国电信';
        }
        if($item['yunying'] == 'guangdian'){
            $item['yunying_text'] = '中国广电';
        }
        $item['total'] = $item['jiben_price']+$item['other_price']+0;
        if(empty($item['taocan_text'])){
            $item['taocan_text'] = '';
        }
        if(empty($item['miaoshu_text'])){
            $item['miaoshu_text'] = '';
        }
        $this->success('请求成功',$item);
    }

    public function content()
    {
        $data = Db::table('fa_content')->where("id",input('id'))->find();
        $this->success('请求成功',$data);
    }

    public function notice()
    {
        $data = Db::table('fa_msg')->where("state",'1')->order('id desc')->paginate()->toArray();
        foreach ($data['data'] as $k=>&$item){
            $item['img'] = request()->domain().'/img/n.png';
        }
        $this->success('请求成功',$data);
    }

    public function suborder()
    {
        $user = $this->auth->getUserinfo();
        /* Db::table('fa_suborder')->where('user_id',input('user_id'))->find();
         Db::table('fa_suborder')->insert();*/
        $input = input();
        if(empty($input['name'])){
            $this->error('请输入姓名');
        }
        if(empty($input['mobile'])){
            $this->error('请输入电话');
        }
        if(!empty($input['address2'])){
            $input['address'] = $input['address2'];
        }
        $input['user_id'] = $user['id'];
        $model = new Suborder($input);
        $model->allowField(true)->save();
        $this->success('提交成功');
    }

    public function wenti()
    {
        $list = Db::table('fa_question')->select();
        $this->success('提交成功',$list);
    }

    public function dengji()
    {
        $input = input();
        $user = $this->auth->getUserinfo();
        if(empty($input['name'])){
            $this->error('开户名');
        }
        if(empty($input['mobile'])){
            $this->error('联系电话');
        }
        $input['createtime'] = time();
        $input['updatetime'] = time();
        $input['user_id'] = $user['id'];
        $model = new Dengji($input);
        $model->allowField(true)->save();
        $this->success('提交成功');
    }


    public function contract()
    {
        $qrcode = cdnurl(config('site.qrcode'));
        $phone = config('site.business_phone');
        $kf_qq = config('site.kf_qq');

        $this->success('', compact('qrcode','phone','kf_qq'));
    }


    public function zu_qrcode()
    {
        $community = $this->request->param('community/d', 1);
        $icon = ConfigValueModel::where([
            'cid' => ConfigValueModel::TYPE_COMMUNITY,
            'option_id' => $community,
            'pid' => 0,
        ])->value('icon', '');
        $data['qrcode'] = cdnurl($icon);
        $this->success('', $data);
    }

    /**
     * 上传文件
     */
    public function upload()
    {
        Config::set('default_return_type', 'json');

        //必须还原upload配置,否则分片及cdnurl函数计算错误
        Config::load(APP_PATH . 'extra/upload.php', 'upload');

        $chunkid = $this->request->post("chunkid");
        if ($chunkid) {
            if (!Config::get('upload.chunking')) {
                $this->error(__('Chunk file disabled'));
            }
            $action = $this->request->post("action");
            $chunkindex = $this->request->post("chunkindex/d");
            $chunkcount = $this->request->post("chunkcount/d");
            $filename = $this->request->post("filename");
            $method = $this->request->method(true);
            if ($action == 'merge') {
                $attachment = null;
                //合并分片文件
                try {
                    $upload = new Upload();
                    $attachment = $upload->merge($chunkid, $chunkcount, $filename);
                } catch (UploadException $e) {
                    $this->error($e->getMessage());
                }
                $this->success(__('Uploaded successful'), '', ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
            } elseif ($method == 'clean') {
                //删除冗余的分片文件
                try {
                    $upload = new Upload();
                    $upload->clean($chunkid);
                } catch (UploadException $e) {
                    $this->error($e->getMessage());
                }
                $this->success();
            } else {
                //上传分片文件
                //默认普通上传文件
                $file = $this->request->file('file');
                try {
                    $upload = new Upload($file);
                    $upload->chunk($chunkid, $chunkindex, $chunkcount);
                } catch (UploadException $e) {
                    $this->error($e->getMessage());
                }
                $this->success();
            }
        } else {
            $attachment = null;
            //默认普通上传文件
            $file = $this->request->file('file');
            try {
                $upload = new Upload($file);
                $attachment = $upload->upload();
            } catch (UploadException $e) {
                $this->error($e->getMessage());
            }

            $this->success(__('Uploaded successful'), '', ['url' => $attachment->url, 'fullurl' => cdnurl($attachment->url, true)]);
        }
    }

    public function qu()
    {
        $list = Db::table('fa_qu')->select();
        $this->success('成功',$list);
    }

    public function jiedao()
    {	
		
		if(!empty(input('qu_id'))){
			$list = Db::table('fa_jiedao')
            ->where('qu_id',input('qu_id'))
            ->select();
			$this->success('成功',$list);
		}else{
			$list = Db::table('fa_jiedao')
            //->where('qu_id',input('qu_id'))
            ->select();
			$this->success('成功',$list);
		}
        
    }

    public function cun()
    {
        if(!empty(input('jiedao_id'))){
			  $list = Db::table('fa_village')
            ->where('jiedao_id',input('jiedao_id'))
            ->select();
			$this->success('成功',$list);
		}else{
			  $list = Db::table('fa_village')
            
            ->select();
			$this->success('成功',$list);
		}
      
    }

    public function check()
    {
        $data =[
            'shequ_check' => config('site.shequ_check'),
        ];
        $this->success('成功',$data);
    }
}
